/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 毛超然 on 2017/09/13 15:11.12
 * <p>
 * 溯源接口类
 */
package com.hw.one.peach.core.controller;

import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.peach.core.service.ITraceAnalysisService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "traceAnalysis")
public class TraceAnalysisController extends BaseController {

    @Resource
    ITraceAnalysisService traceAnalysisService;

    /**
     * 根据站点code和时间查询溯源追踪的数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "findTraceDataBySiteCodeAndTime", method = RequestMethod.GET)
    public MsgResult findTraceDataBySiteCodeAndTime(HttpServletRequest request) {
        String siteCode = request.getParameter("siteCode");
        if (null == siteCode || "".equals(siteCode)) {
            return MsgResult.infoResult("查询条件点位编码siteCode为空，查询失败。");
        }
        String occurTime = request.getParameter("occurTime");

        String dataType = request.getParameter("dataType");
        if (null == dataType || "".equals(dataType)) {
            dataType = "real";
        }
        Map<String, Object> paramMap = new HashedMap();
        paramMap.put("siteCode", siteCode);
        paramMap.put("dataType", dataType);
        paramMap.put("occurTime", occurTime);
        try {
            Map<String, Object> returnMap = traceAnalysisService.findTraceabilityData(paramMap);
            return MsgResult.okResult(returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }


    /**
     * 实时生成并查询溯源追踪的数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "findRealTraceDataBySiteCodeAndTime", method = RequestMethod.GET)
    public MsgResult findRealTraceDataBySiteCodeAndTime(HttpServletRequest request) {
        String siteCode = request.getParameter("siteCode");
        if (null == siteCode || "".equals(siteCode)) {
            return MsgResult.infoResult("查询条件点位编码siteCode为空，查询失败。");
        }
        String occurTime = request.getParameter("occurTime");
        if (null == occurTime || "".equals(occurTime)) {
            return MsgResult.infoResult("查询条件报警时间time为空，查询失败。");
        }
        Map<String, Object> paramMap = new HashedMap();
        paramMap.put("siteCode", siteCode);
        paramMap.put("occurTime", occurTime);
        try {
            return traceAnalysisService.findRealTraceabilityDatas(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

    /**
     * 根据siteCode和日期查询一段时间内的溯源追踪的数据
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "findTraceDatasBySiteCodeAndTime", method = RequestMethod.GET)
    public MsgResult findTraceDatasBySiteCodeAndTime(HttpServletRequest request) {
        String siteCode = request.getParameter("siteCode");
        if (null == siteCode || "".equals(siteCode)) {
            return MsgResult.infoResult("查询条件点位编码siteCode为空，查询失败。");
        }
        String occurTime = request.getParameter("occurTime");
        if (null == occurTime || "".equals(occurTime)) {
            return MsgResult.infoResult("查询条件查询时间time为空，查询失败。");
        }
        Map<String, Object> paramMap = new HashedMap();
        paramMap.put("siteCode", siteCode);
        paramMap.put("monitorTime", occurTime);
        try {
            List<Map<String, Object>> returnList = traceAnalysisService.findTraceabilityDatas(paramMap);
            return MsgResult.okResult(returnList);
        } catch (Exception e) {
            e.printStackTrace();
            return MsgResult.exceptionResult();
        }
    }

}
